package com.ble;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Base64;
import android.util.Log;

 
public class BlueToothPara{
	public static final int GAP_ADTYPE_FLAGS = 0x01; //!< Discovery Mode: @ref GAP_ADTYPE_FLAGS_MODES
	public static final int GAP_ADTYPE_16BIT_MORE = 0x02; //!< Service: More 16-bit UUIDs available
	public static final int GAP_ADTYPE_16BIT_COMPLETE = 0x03; //!< Service: Complete list of 16-bit UUIDs
	public static final int GAP_ADTYPE_32BIT_MORE = 0x04; //!< Service: More 32-bit UUIDs available
	public static final int GAP_ADTYPE_32BIT_COMPLETE = 0x05; //!< Service: Complete list of 32-bit UUIDs
	public static final int GAP_ADTYPE_128BIT_MORE = 0x06; //!< Service: More 128-bit UUIDs available
	public static final int GAP_ADTYPE_128BIT_COMPLETE =  0x07; //!< Service: Complete list of 128-bit UUIDs
	public static final int GAP_ADTYPE_LOCAL_NAME_SHORT =  0x08; //!< Shortened local name
	public static final int GAP_ADTYPE_LOCAL_NAME_COMPLETE =  0x09; //!< Complete local name
	public static final int GAP_ADTYPE_POWER_LEVEL = 0x0A; //!< TX Power Level: 0xXX: -127 to +127 dBm
	public static final int GAP_ADTYPE_OOB_CLASS_OF_DEVICE =  0x0D; //!< Simple Pairing OOB Tag: Class of device (3 octets)
	public static final int GAP_ADTYPE_OOB_SIMPLE_PAIRING_HASHC = 0x0E; //!< Simple Pairing OOB Tag: Simple Pairing Hash C (16 octets)
	public static final int GAP_ADTYPE_OOB_SIMPLE_PAIRING_RANDR = 0x0F; //!< Simple Pairing OOB Tag: Simple Pairing Randomizer R (16 octets)
	public static final int GAP_ADTYPE_SM_TK = 0x10; //!< Security Manager TK Value
	public static final int GAP_ADTYPE_SM_OOB_FLAG = 0x11; //!< Secutiry Manager OOB Flags
	public static final int GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE = 0x12; //!< Min and Max values of the connection interval (2 octets Min, 2 octets Max) (0xFFFF indicates no conn interval min or max)
	public static final int GAP_ADTYPE_SIGNED_DATA = 0x13; //!< Signed Data field
	public static final int GAP_ADTYPE_SERVICES_LIST_16BIT = 0x14; //!< Service Solicitation: list of 16-bit Service UUIDs
	public static final int GAP_ADTYPE_SERVICES_LIST_128BIT = 0x15; //!< Service Solicitation: list of 128-bit Service UUIDs
	public static final int GAP_ADTYPE_SERVICE_DATA = 0x16; //!< Service Data
	public static final int GAP_ADTYPE_APPEARANCE = 0x19; //!< Appearance
	public static final int GAP_ADTYPE_MANUFACTURER_SPECIFIC = 0xFF; //!< Manufacturer Specific Data: first 2 octets contain the Company Identifier Code followed by the additional manufacturer specific data
  
	
	public static final int SEARCH_IBEACON_DEVICE = 0x01;
	public static final int SEARCH_AIRSYNC_DEVICE = 0x02;
	
	/** @} End GAP_ADTYPE_DEFINES */
	private int search_mode = SEARCH_AIRSYNC_DEVICE;
	private JSONObject ble = null; 
	
	public BlueToothPara(String address){
		ble = new JSONObject();
		try {
			ble.put("MAC_ADDRESS", address);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	
	
	public void SetSearchMode(int searchmode){
		search_mode = searchmode;
	}
	 
	public BlueToothPara()
	{  
//		putdata(data);
	}
	
	public Boolean putdata(byte[] data)
	{
		int i = 0;
		try {
			
			JSONArray serviceUUID = new JSONArray();

			while(i < data.length)
			{
				int len = data[i];
				if(len == 0x00)
					break;
				int bletype = data[i+1]&0xff;
				byte[] bledata = new byte[len-1];
				System.arraycopy(data,i+2,bledata,0,len-1);
				Log.d("step",String.valueOf(data[i]));
				switch (bletype) {
					case GAP_ADTYPE_FLAGS:
							
						break;
					case GAP_ADTYPE_16BIT_MORE:
						
					case GAP_ADTYPE_16BIT_COMPLETE:
						
					case GAP_ADTYPE_32BIT_MORE:
						
		 			case GAP_ADTYPE_32BIT_COMPLETE:
						 //!< Service: Complete list of 32-bit UUIDs
					case GAP_ADTYPE_128BIT_MORE:
						 //!< Service: More 128-bit UUIDs available
					case GAP_ADTYPE_128BIT_COMPLETE:
						
						break; //!< Service: Complete list of 128-bit UUIDs
					case GAP_ADTYPE_LOCAL_NAME_SHORT:	
						
						ble.put("LOCAL_NAME_SHORT", new String(bledata));
						break; //!< Shortened local name
					case GAP_ADTYPE_LOCAL_NAME_COMPLETE:
						Log.d("test", new String(bledata));
						ble.put("LOCAL_NAME_COMPLETE", new String(bledata));
						break; //!< Complete local name
					case GAP_ADTYPE_POWER_LEVEL:
						ble.put("POWER_LEVEL", bledata[0]);
						break; //!< TX Power Level: 0xXX: -127 to +127 dBm
					case GAP_ADTYPE_OOB_CLASS_OF_DEVICE:
						break; //!< Simple Pairing OOB Tag: Class of device (3 octets)
					case GAP_ADTYPE_OOB_SIMPLE_PAIRING_HASHC:
						break;//!< Simple Pairing OOB Tag: Simple Pairing Hash C (16 octets)
					case GAP_ADTYPE_OOB_SIMPLE_PAIRING_RANDR:
						break;//!< Simple Pairing OOB Tag: Simple Pairing Randomizer R (16 octets)
					case GAP_ADTYPE_SM_TK:  
						break;//!< Security Manager TK Value
					case GAP_ADTYPE_SM_OOB_FLAG:
						break;//!< Secutiry Manager OOB Flags
					case GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE:
						break;//!< Min and Max values of the connection interval (2 octets Min, 2 octets Max) (0xFFFF indicates no conn interval min or max)
					case GAP_ADTYPE_SIGNED_DATA:
						break;//!< Signed Data field
					case GAP_ADTYPE_SERVICES_LIST_16BIT:
						break;//!< Service Solicitation: list of 16-bit Service UUIDs
					case GAP_ADTYPE_SERVICES_LIST_128BIT:
						break;//!< Service Solicitation: list of 128-bit Service UUIDs
					case GAP_ADTYPE_SERVICE_DATA:
//						ble.put("SERVICE_DATA", BlueToothConfig.bytesToString(turn(bledata), len-1));
						break;//!< Service Data
					case GAP_ADTYPE_APPEARANCE:
						break;//!< Appearance
					case GAP_ADTYPE_MANUFACTURER_SPECIFIC:
						Log.d("test","enter");
						if(parity(bledata) == true)
							ble.put("MANUFACTURER_SPECIFIC",Base64.encodeToString(bledata, Base64.DEFAULT));
						else 
							return false;
						break;
					default:
						break; 
				}
				i = i + len + 1;
			} 
			Log.d("ble",ble.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(); 
		}
		return true; 
	}   
	
	private Boolean parity(byte[] data)
	{
		int i = 0; 
//        for(int j=0 ; j<data.length; j++)
//        	Log.d("test", String.valueOf(data[j]));
    	if(data[0] != 0x68) 
    		return false;  
    	
//    	if(data[1] == 0x01)
//    		;  
//    	else if(data[1] == 0x02) 
//    		;
//    	else if(data[1] == 0x03)
//    		;
//    	else
//    		return false;
 
    	byte parity = 0x00;  
    	int len = 11+data[4];
    	for(int j = 1 ; j < len; j++) 
    	{
    		parity += data[j];
    	} 
    	Log.d("test", String.valueOf(parity) + data[len]);
    	if(parity == data[len])
    	{ 
    		Log.w("parity", "parity is success"); 
    	}else{
    		Log.w("parity", "parity is failed"); 
    		return false;
    	}
  
    	return true;     
	} 
	 
	private byte[] turn(byte[] data){
		byte[] newdata = new byte[data.length];
		for(int i=0 ; i < data.length ; i++)
			newdata[i] = data[data.length-1-i];
		return newdata;
	} 
	
	public String blejson(){
		Log.w("blejson", ble.toString());
		return ble.toString();
	}
	
}